import { lib, game, ui, get, ai, _status } from '../../../noname.js';
/*import { swTool } from '../main/swTool.js'*/
ui.create.connectPlayers = function (ip) {
	ui.updateConnectPlayerPositions();
	game.connectPlayers = [];
	const configOL = lib.configOL;
	const numberOfPlayers = parseInt(configOL.player_number) || configOL.number;
	for (let position = 0; position < numberOfPlayers; position++) {
		const player = ui.create.player(ui.window);
		player.dataset.position = position;
		player.classList.add("connect");
		game.connectPlayers.push(player);
	}

	var bar = ui.create.div(ui.window);
	bar.style.height = "20px";
	bar.style.width = "80%";
	bar.style.left = "10%";
	bar.style.top = "calc(200% / 7 - 120px + 5px)";
	bar.style.textAlign = "center";
	var ipbar = ui.create.div(".shadowed", ip, bar);
	ipbar.style.padding = "4px";
	ipbar.style.borderRadius = "2px";
	ipbar.style.position = "relative";

	var button = ui.create.div(".menubutton.large.highlight.connectbutton.connectbutton1.pointerdiv", game.online ? "退出联机" : "开始游戏", ui.window, function () {
		if (button.clicked) return;
		if (game.online) {
			if (game.onlinezhu) {
				game.send("startGame");
			} else {
				game.saveConfig("tmp_owner_roomId");
				game.saveConfig("tmp_user_roomId");
				game.saveConfig("reconnect_info");
				game.reload();
			}
		} else {
			// var num = 0;
			// for (var i of game.connectPlayers) {
			//     if (
			//         !i.nickname &&
			//         !i.classList.contains("unselectable2")
			//     )
			//         num++;
			// }
			// if (num >= lib.configOL.number - 1) {
			//     alert("至少要有两名玩家才能开始游戏！");
			//     return;
			// }
			game.resume();
		}
		button.delete();
		bar.delete();
		shareButton.delete();
		delete ui.connectStartButton;
		delete ui.connectStartBar;
		delete ui.connectShareButton;
		button.clicked = true;
	});

	var shareButton = ui.create.div(".menubutton.large.highlight.connectbutton.connectbutton2.pointerdiv", "分享房间", ui.window, function () {
		var text = `无名杀-联机-${lib.translate[get.mode()]}-${game.connectPlayers.filter(p => p.avatar).length}/${game.connectPlayers.filter(p => !p.classList.contains("unselectable2")).length}\n${get.connectNickname()}邀请你加入${game.roomId}房间\n联机地址:${game.ip}\n请先通过游戏内菜单-开始-联机中启用“读取邀请链接”选项`;
		window.focus();
		if (navigator.clipboard && lib.node) {
			navigator.clipboard
				.writeText(text)
				.then(() => {
					game.alert(`分享内容复制成功`);
				})
				.catch(e => {
					game.alert(`分享内容复制失败${e || ""}`);
				});
		} else {
			var input = ui.create.node("textarea", ui.window, {
				opacity: "0",
			});
			input.value = text;
			input.focus();
			input.select();
			var result = document.execCommand("copy");
			input.blur();
			ui.window.removeChild(input);
			game.alert(`分享内容复制${result ? "成功" : "失败"}`);
		}
	});

	ui.connectStartButton = button;
	ui.connectStartBar = bar;
	ui.connectShareButton = shareButton;
};
